package com.example.custermbarview;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

public class MainActivity extends Activity implements OnPageChangeListener {

	private ArrayList<View> mList;
	private TextView tvHel;
	private ViewPager guidePager;
	
	private int currentItemIndext=0;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		tvHel = (TextView) findViewById(R.id.tvHel);
		
		guidePager = (ViewPager) findViewById(R.id.vp_main);
		mList = new ArrayList<View>();
		mList.add(new CustermBarView(this));
		mList.add(new CustermBarView(this));
		mList.add(new CustermBarView(this));
		MyViewPagerAdapter mAdapter = new MyViewPagerAdapter(mList);
		guidePager.setAdapter(mAdapter);
		guidePager.setOnPageChangeListener(this);
	}
	
	public void doNext(View v){
		if(currentItemIndext<mList.size()-1){
			currentItemIndext++;
			guidePager.setCurrentItem(currentItemIndext,true);
		}
	}
	
	
	public void doPre(View v){
		if(currentItemIndext>0){
			currentItemIndext--;
			guidePager.setCurrentItem(currentItemIndext,true);
		}
	}
	/**
	 * ViewPager适配器
	 */
	class MyViewPagerAdapter extends PagerAdapter {
		private List<View> mListViews;

		public MyViewPagerAdapter(List<View> mListViews) {
			this.mListViews = mListViews;// 构造方法，参数是我们的页卡，这样比较方便。
		}

		@Override
		public void destroyItem(ViewGroup container, int position, Object object) {
			container.removeView(mListViews.get(position));// 删除页卡
		}

		@Override
		public Object instantiateItem(ViewGroup container, int position) { // 这个方法用来实例化页卡
			container.addView(mListViews.get(position), 0);// 添加页卡
			return mListViews.get(position);
		}

		@Override
		public int getCount() {
			return mListViews.size();// 返回页卡的数量
		}

		@Override
		public boolean isViewFromObject(View arg0, Object arg1) {
			return arg0 == arg1;// 官方提示这样写
		}
	}

	@Override
	public void onPageScrollStateChanged(int arg0) {
		// TODO Auto-generated method stub
	}

	@Override
	public void onPageScrolled(int arg0, float arg1, int arg2) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onPageSelected(int pageIndex) {
		currentItemIndext=pageIndex;
		tvHel.setText("页面:"+pageIndex);
	}
}
